package utils.security; /**
 * Created by Administrator on 2015/12/25.
 */

import net.jcip.annotations.ThreadSafe;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

@ThreadSafe
public class MD5Util {
    private static char hexDigits[] = { '0', '1', '2', '3', '4',
            '5', '6', '7', '8', '9',
            'A', 'B', 'C', 'D', 'E', 'F' };
    //获得MD5摘要算法的 MessageDigest 对象
    private static ThreadLocal<MessageDigest> mdInst = new ThreadLocal(){
        @Override
        protected Object initialValue() {
            try {
                return MessageDigest.getInstance("MD5");
            } catch (NoSuchAlgorithmException e) {
                e.printStackTrace();
                return null;
            }
        }
    };

    public static String getMd5String(String... s) {
        try {
            MessageDigest messageDigest = mdInst.get();
            for (String s1 : s) {
                byte[] btInput = s1.getBytes();
                //使用指定的字节更新摘要
                messageDigest.update(btInput);
            }
            return str(messageDigest);
        }
        catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public static String getMd5String(File file) {
        try(InputStream inputStream = new FileInputStream(file)) {
            return getMd5String(inputStream);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static String getMd5String(InputStream inputStream) {
        try {
            System.out.println(inputStream.available());
            MessageDigest messageDigest = mdInst.get();
            int bufferSize = 1024;
            byte[] bytes = new byte[bufferSize];
            while(inputStream.read(bytes) > 0) {
                messageDigest.update(bytes);
            }
            return str(messageDigest);
        }
        catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public static String getMd5String(byte[] bytes) {
        try {
            System.out.println(bytes.length);
            MessageDigest messageDigest = mdInst.get();
            messageDigest.update(bytes);
            return str(messageDigest);
        }
        catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }


    private static String str(MessageDigest messageDigest) {
        //获得密文
        byte[] md = messageDigest.digest();
        //把密文转换成十六进制的字符串形式
        int j = md.length;
        char str[] = new char[j << 1];
        int k = 0;
        for (int i = 0; i < j; i++) {
            byte byte0 = md[i];
            str[k++] = hexDigits[(240 & byte0) >>> 4];
            str[k++] = hexDigits[15 & byte0];
        }
        return new String(str);
    }
    public static void main(String[] args) {
//        ExecutorService exec = Executors.newCachedThreadPool();
//        for (int i = 0; i < 100; i++) {
//            exec.submit(() -> {
//                System.out.println(MD5Util.getMD5String("小智"));
//                System.out.println(MD5Util.getMD5String("小", "智"));
//                System.out.println(MD5Util.getMD5String("D/123"));
//                System.out.println(MD5Util.getMD5String("D", "/", "1", "2", "3"));
//            });
//        }
        System.out.println(getMd5String("654321"));
    }
}